home *** CD-ROM | disk | FTP | other *** search
Text File | 1997-06-06 | 11.2 KB | 398 lines | [TEXT/CWIE] |
- //========================================================================
- // Application: QTICSampleApp
- //
- // Description: This application demonstrates the integration of
- // QuickTime™ IC functionality.
- //
- // Author: Mike Bitz
- // Developer Technical Support
- // Apple Computer, Inc.
- //
- // History: 25-Apr-97 original development (mwb)
- //
- // Copyright © 1997 Apple Computer, Inc., All Rights Reserved
- //
- // You may incorporate this sample code into your applications without
- // restriction, though the sample code has been provided "AS IS" and the
- // responsibility for its operation is 100% yours. However, what you are
- // not permitted to do is to redistribute the source as "DSC Sample Code"
- // after having made changes. If you're going to re-distribute the source,
- // we require that you make it clear in the source that the code was
- // descended from Apple Sample Code, but that you've made changes.
- //========================================================================
-
- #include "prototypes.h"
- #include "application.h"
- #include <QTIC.h>
- #include "IterateDirectory.h" // from MoreFiles
- #include <PLStringFuncs.h>
-
- extern QTICControls gCameraControls;
- extern QTICActionFilterWithRefConUPP gCallBack;
- extern MenuHandle gPanelSpecificMenu;
- extern Component gPanelComponents[10];
- extern short gNumPanels, gNumPrefPanels;
-
-
- //================= myIterateDirectory =================
- //
- // We use this function to help us register 'qtic' components
- // at start-up time. It's passed as a parameter to
- // FSpIterateDirectory below.
- //
- pascal void myIterateDirectory(const CInfoPBRec * const cpbPtr, Boolean *quitFlag, void *yourDataPtr)
- {
- short registerResRefNum;
- FSSpec myFSSpec;
-
- if ( (cpbPtr->hFileInfo.ioFlAttrib & ioDirMask) == 0 && cpbPtr->hFileInfo.ioFlFndrInfo.fdType == 'qtic' ) {
- if ( FSMakeFSSpec(cpbPtr->hFileInfo.ioVRefNum, cpbPtr->hFileInfo.ioFlParID, cpbPtr->hFileInfo.ioNamePtr,
- &myFSSpec) == noErr ) {
- registerResRefNum = FSpOpenResFile(&myFSSpec, fsRdPerm);
- if ( registerResRefNum != -1 ) {
- RegisterComponentResourceFile(registerResRefNum, 0);
- CloseResFile(registerResRefNum);
- }
- }
- }
- }
-
- //================= InitQTIC =================
- //
- // This is called when the application starts up. We use FSpIterateDirectory
- // which is available with MoreFiles. Check your Developer Tool Chest CD
- // for this fine file system library.
- //
- void InitQTIC (void)
- {
- OSErr err = noErr;
- FCBPBRec pb;
- short resFile;
- FSSpec myFSSpec;
-
- // Load any components in the folder of the application.
- pb.ioNamePtr = myFSSpec.name;
- pb.ioVRefNum = 0;
- pb.ioRefNum = CurResFile();
- pb.ioFCBIndx = 0;
- if (PBGetFCBInfoSync(&pb) == noErr)
- {
- resFile = CurResFile();
-
- myFSSpec.vRefNum = pb.ioFCBVRefNum;
- myFSSpec.parID = pb.ioFCBParID;
- myFSSpec.name[0] = 0;
- FSpIterateDirectory(&myFSSpec, 3, myIterateDirectory, nil);
- UseResFile(resFile);
- }
-
- // Open a QTIC camera controls component
- gCameraControls = OpenDefaultComponent (kQTICControlsComponentType, kQTICControlsComponentSubType);
- if (gCameraControls != nil)
- {
- // You must call QTICCInitialize immediately after you open the camera controls component
- err = QTICCInitialize (gCameraControls, nil);
- if (err != noErr)
- {
- CloseComponent (gCameraControls);
- gCameraControls = nil;
-
- // Use the QTIC facilities to report the error
- QTICCReportError (gCameraControls, "\pQTICInitialize failed", err);
- }
-
- // Set up a callback function so we can override actions
- gCallBack = NewQTICActionFilterWithRefConProc (CameraCallBack);
- if (gCallBack != nil)
- {
- // Install an action filter (these are called in the order in which they are installed).
- if (QTICCInstallActionFilter (gCameraControls, gCallBack, 0) != noErr)
- {
- CloseComponent (gCameraControls);
- gCameraControls = nil;
- }
- }
- else
- {
- CloseComponent (gCameraControls);
- gCameraControls = nil;
- }
- }
- else
- {
- // Use the QTIC facilities to report the error
- QTICCReportError (gCameraControls, "\pControls Component not opened", err);
- }
- }
-
- //================= CameraCallBack =================
- pascal Boolean CameraCallBack (QTICControls qtcc, short action, void *param1, void *param2, void *param3, void *param4, long refCon)
- {
- Boolean handled = false;
-
- // Don't override anything for now.
- switch (action)
- {
- default:
- break;
- }
-
- return (handled);
- }
-
- //================= OpenMainPanel =================
- OSErr OpenMainPanel (void)
- {
- OSErr err = noErr;
-
- if (gCameraControls != nil)
- {
- // If the panel is already open, don't bother opening it again.
- err = QTICCDoAction(gCameraControls, kQTICActionIsPanelOpenByFlag, (void *) kQTICP_MainPanelFlag, nil, nil, nil);
- if (err == 1)
- {
- // The panel is not open, so we'll open it now.
- err = QTICCDoAction(gCameraControls, kQTICActionOpenPanelByFlag, (void *) kQTICP_MainPanelFlag, nil, nil, nil);
- }
- }
-
- // Enable Close Window item if a window is in the foreground
- UpdateCloseWindowItem ();
-
- return err;
- }
-
- //================= CloseAppWindow =================
- OSErr CloseAppWindow (void)
- {
- OSErr err = noErr;
- WindowPtr theFrontWindow = nil;
- MenuHandle hMenu = nil;
-
- theFrontWindow = FrontWindow ();
- if (theFrontWindow != nil)
- {
- if (gCameraControls != nil)
- {
- //It's a QTIC window, close it properly
- if (theFrontWindow != nil && QTICCIsPanel (gCameraControls, theFrontWindow, nil) != 0)
- QTICCCloseWindow (gCameraControls, theFrontWindow);
- else
- // otherwise we'll close it the normal way
- CloseWindow (theFrontWindow);
- }
-
- // Disable the Close Window menu item if no windows are currently visible.
- UpdateCloseWindowItem ();
- }
- return err;
- }
-
- //================= NumberOfInstalledCameras =================
- //
- // Return the number of 'cmra' components which are installed.
- //
- short NumberOfInstalledCameras (void)
- {
- short currentCount = 0;
- ComponentDescription compDesc;
- Component theComponent = nil;
-
- compDesc.componentType = kQTICCameraComponentType;
- compDesc.componentSubType = 0;
- compDesc.componentManufacturer = 0;
- compDesc.componentFlags = 0L;
- compDesc.componentFlagsMask = 0L;
-
- do
- {
- theComponent = FindNextComponent (theComponent, &compDesc);
- if (theComponent != nil)
- {
- currentCount++;
- }
- }
- while (theComponent != nil);
-
- return currentCount;
- }
-
- //================= UpdatePanelSpecificMenu =================
- //
- // When a panel is the frontmost window, we need to
- // update our custom menu item to reflect the panel's
- // capabilities.
- //
- void UpdatePanelSpecificMenu (void)
- {
- OSErr err = noErr;
- Str255 name;
- WindowPtr frontWindow = nil;
-
- if (gPanelSpecificMenu != nil)
- {
- DeleteMenu (menuTemp);
- DisposeHandle ((Handle)gPanelSpecificMenu);
- gPanelSpecificMenu = nil;
- }
-
- frontWindow = FrontWindow ();
-
- if (frontWindow != nil)
- {
- // Get the menu name from the window
- err = QTICCGetMenuName (gCameraControls, frontWindow, name);
- if (err == noErr)
- {
- // Create a new menu item
- gPanelSpecificMenu = NewMenu (menuTemp, name);
- if (gPanelSpecificMenu != nil)
- {
- // Allow the panel that owns the window to update the custom menu
- err = QTICCUpdateCustomMenu (gCameraControls, frontWindow, gPanelSpecificMenu);
- if (err == noErr)
- {
- // Insert menu items if there are any
- if (CountMItems (gPanelSpecificMenu) > 0 )
- {
- InsertMenu (gPanelSpecificMenu, 0);
- }
- // otherwise remove the menu because it would not provide any benefit to the user
- else
- {
- DisposeHandle ((Handle)gPanelSpecificMenu);
- gPanelSpecificMenu = nil;
- }
- }
- }
- }
- }
-
- // Update the Close Window menu item since we might have closed all the windows
- UpdateCloseWindowItem ();
-
- DrawMenuBar ();
- }
-
- //================= UpdatePanelListMenu =================
- //
- // Create a menu list of all available panel components.
- //
- void UpdatePanelListMenu (void)
- {
- OSErr err = noErr;
- MenuHandle hPanelListMenu = nil;
- short i, numPanels;
- Component **hPanelComponents = nil;
- ComponentDescription compDesc;
- Handle componentName = nil;
- Str255 menuString;
-
- if (gCameraControls != nil)
- {
- gNumPanels = 0;
- gNumPrefPanels = 0;
-
- // Get a handle to the menu record, remove all of its items
- hPanelListMenu = GetMHandle (menuPanels);
- for (i = CountMItems (hPanelListMenu); i>0; i--)
- {
- DeleteMenuItem (hPanelListMenu, 1);
- }
-
- // Get the number of installed panels that will work with the selected device.
- err = QTICCGetNumPanels (gCameraControls, &numPanels);
- if (numPanels > 0 && err == noErr)
- {
- // Allocate memory for the panel components list
- hPanelComponents = (Component**)NewHandle (sizeof (Component) * numPanels);
- if (hPanelComponents != nil)
- {
- MoveHHi ((Handle)hPanelComponents);
- HLock ((Handle) hPanelComponents);
-
- // Get the list of installed panel components
- err = QTICCGetPanels (gCameraControls, (Component*) *hPanelComponents);
- if (err == noErr)
- {
- // Show/hide/main panels
- for (i=0; i<numPanels; i++)
- {
- componentName = NewHandle (4);
- if (componentName != nil)
- {
- err = GetComponentInfo ((*hPanelComponents)[i], &compDesc, componentName, nil, nil);
- if (err == noErr)
- {
- HLock (componentName);
-
- if (compDesc.componentType == kQTICPanelComponentType &&
- (compDesc.componentFlags & kQTICP_ShowHidePanelFlag) &&
- (compDesc.componentFlags & kQTICP_MainPanelFlag))
- {
- AppendMenu (hPanelListMenu, (StringPtr)*componentName);
- gPanelComponents [gNumPanels] = (*hPanelComponents)[i];
- gNumPanels++;
- }
- }
- }
- DisposeHandle (componentName);
- }
-
- // Show/hide/not main panels
- for (i=0; i<numPanels; i++)
- {
- componentName = NewHandle (4);
- if (componentName != nil)
- {
- err = GetComponentInfo ((*hPanelComponents)[i], &compDesc, componentName, nil, nil);
- if (err == noErr)
- {
- HLock (componentName);
-
- if (compDesc.componentType == kQTICPanelComponentType &&
- (compDesc.componentFlags & kQTICP_ShowHidePanelFlag) &&
- !(compDesc.componentFlags & kQTICP_MainPanelFlag))
- {
- AppendMenu (hPanelListMenu, (StringPtr)*componentName);
- gPanelComponents [gNumPanels] = (*hPanelComponents)[i];
- gNumPanels++;
- }
- }
- }
- DisposeHandle (componentName);
- }
-
- // Seperate the other panels from the preferences
- AppendMenu (hPanelListMenu, "\p-");
- gNumPrefPanels++;
-
- // Preference panels
- for (i=0; i<numPanels; i++)
- {
- componentName = NewHandle(4);
- if (componentName != nil)
- {
- err = GetComponentInfo((*hPanelComponents)[i], &compDesc, componentName, nil, nil);
- if (err == noErr)
- {
- HLock (componentName);
-
- if (compDesc.componentType == kQTICPanelComponentType &&
- (compDesc.componentFlags & kQTICP_NeedsNewMenuFlag) &&
- !(compDesc.componentFlags & kQTICP_ShowHidePanelFlag))
- {
- AppendMenu(hPanelListMenu, (StringPtr)*componentName);
- gPanelComponents [gNumPanels+gNumPrefPanels] = (*hPanelComponents)[i];
- gNumPrefPanels++;
- }
- }
- }
- DisposeHandle (componentName);
- }
- }
- DisposeHandle ((Handle)hPanelComponents);
- }
- }
- }
- }